package com.hzx.service;

import com.hzx.Activity;
import com.hzx.mapper.ActivityMapper;
import com.hzx.util.RandomString;
import com.hzx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Service
public class ActivityService {

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    RedisUtil redisUtil;

    public Activity activityDetailSearch(int aId) {
        return activityMapper.searchActivity(aId);
    }

    public List<Activity> activitySearch() {
        return activityMapper.searchAll();
    }

    public boolean addActivity(String title, String text, int year, int month, int day, int hour, int num) {
        Activity activity = new Activity();
        activity.setNum(num);
        activity.setText(text);
        activity.setTitle(title);
        Date date = new Date(year - 1900, month - 1, day, hour + 8, 0);
        activity.setStartTime(date);
        activityMapper.insert(activity);
        generateToken(activity.getAId(), num, date);
        return true;
    }

    @Async
    void generateToken(int aId, int num, Date time) {
        List<Object> stringList = RandomString.getString(aId, num);
        redisUtil.lSet("aid" + aId, stringList);
        redisUtil.set("time" + aId, time);
    }
}
